// 2. 字符串
/**
 * 1. C风格字符串(定义)
 * 2. #21 C风格字符串方法（strcpy、strcat、strlen、strcmp、strchr、strstr）
 * 3. C++中的String类（可以使用+拼接、=复制）
 * 4. String类方法（length、size、substr、replace、find、find-first-of、find-last-of、insert、append、）
 * 
 * 扩展:
 * 1. 字符串的自动连接
 * 2. 字符串分隔符\
 * 3. R字符串( 格式: R"(字符串内容--转义字符不生效)" 、<对称>分隔符使用 R"#%^&*(1231)"123)#%^&*"  ) 与 字符串前缀( L: wchar_t 、 u: char16_t 、 U: char32_t 、 u8: char)
 * 
*/

#include <iostream>
#define __mark 21

/*
 * C风格字符串方法
*/
#if __mark == 21
#include <string.h>
int main(void)
{
  char s1[] = {'t', 'e', 's', 't', '\0'}; // 这是字符串
  char s2[] = {'t', 'e', 's', 't'};       // 这是字符数组
  char s3[] = "test";                     // 这是字符串
  char temp[5];

  strcpy(temp, s1);

  std::cout << "temp : " << temp << std::endl;

  std::cout << "s1 length: " << strlen(s1) << std::endl;
  std::cout << "s2 length: " << strlen(s2) << std::endl; // 8
  std::cout << "s3 length: " << strlen(s3) << std::endl;
  std::cout << "temp length: " << strlen(temp) << std::endl;

  if (strcmp(s1, s3) == 0)
  {
    std::cout << "s1 is equal to s3" << std::endl;
  }
  else
  {
    std::cout << "s1 is not equal to s3" << std::endl;
  }

  if (strcmp(s2, temp) == 0)
  {
    std::cout << "s2 is equal to temp" << std::endl;
  }
  else
  {
    std::cout << "s2 is not equal to temp" << std::endl;
  }

  char tmp[30] = "";
  strcat(tmp, s1);
  // strcat(tmp, s2); // testtesttest ???!!
  strcat(tmp, s3);
  std::cout << "tmp : " << tmp << std::endl;

  strcpy(tmp, "这本书的主人公是罗伯特");

  char *p = strstr(tmp, "主人公是");

  std::cout << " p : " << ((void *)p) << std::endl;

  char role[30];
  // p += 4; // 这样不行,好像是中文的原因 公是罗伯特
  strcpy(role, p);

  std::cout << "role : " << role << std::endl;

  p = nullptr;
  p = strchr(tmp, '罗');                 //  warning: multi-character character constant [-Wmultichar]
  std::cout << "p : " << p << std::endl; // 异常

  strcpy(tmp, "The book's role is Robert");
  p = strstr(tmp, "role is");

  p += 7;

  std::cout << "role : " << p << std::endl;

  p = nullptr;
  p = strchr(tmp, 'R');
  std::cout << "role : " << p << std::endl;

  printf("-------------end----------------\n");
  // system("pause");
  return 0;
}

#elif __mark == 'z'

int main()
{

  printf("-------------end----------------\n");
  // system("pause");
  return 0;
}

#endif